/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Rate.java
 *
 * Created on 15-08-2011, 13:59:30
 */
package tuitionmanagement.gui;

import Business.RateBuss;


import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tuitionmanagement.DAO.RateDao;
import tuitionmanagement.entity.RateEntity;

/**
 *
 * @author XUAN
 */
public class Rate extends javax.swing.JPanel {

    Vector vRow = new Vector();
    ArrayList<RateEntity> lst;
    RateEntity rat = new RateEntity();
    RateBuss ratebuss = new RateBuss();
    RateDao ratedao = new RateDao();
    public int confirm;
    private String lastError;

    /** Creates new form Rate */
    public Rate() {
        initComponents();
        dptDateSearch.setDate(new Date());
        loadTableRate();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtUSD = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtVietnam = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        dtpDate = new com.toedter.calendar.JDateChooser();
        jPanel3 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbRate = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        btnDone = new javax.swing.JButton();
        dptDateSearch = new com.toedter.calendar.JDateChooser();
        jPanel6 = new javax.swing.JPanel();
        txtMeaseage = new javax.swing.JLabel();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 0, 255));
        jLabel1.setText("Rate");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 35, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 6, 5);
        add(jPanel1, gridBagConstraints);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        txtUSD.setMinimumSize(new java.awt.Dimension(150, 20));
        txtUSD.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtUSD, gridBagConstraints);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("USD:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel7, gridBagConstraints);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Vietnam:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel8, gridBagConstraints);

        txtVietnam.setMinimumSize(new java.awt.Dimension(150, 20));
        txtVietnam.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtVietnam, gridBagConstraints);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("DateRate:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel9, gridBagConstraints);

        dtpDate.setDateFormatString("dd/MM/yyyy");
        dtpDate.setMinimumSize(new java.awt.Dimension(145, 20));
        dtpDate.setPreferredSize(new java.awt.Dimension(145, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 15, 5, 0);
        jPanel2.add(dtpDate, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 6, 6, 5);
        add(jPanel2, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 6, 5);
        jPanel3.add(btnAdd, gridBagConstraints);

        btnEdit.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 6, 5);
        jPanel3.add(btnEdit, gridBagConstraints);

        btnDelete.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 6, 5);
        jPanel3.add(btnDelete, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(jPanel3, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        tbRate.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "USD", "Vietnam", "DateRate"
            }
        ));
        tbRate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbRateMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbRate);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 250;
        gridBagConstraints.ipady = 100;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel4.add(jScrollPane1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        add(jPanel4, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Date:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel5.add(jLabel2, gridBagConstraints);

        btnDone.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDone.setText("Done");
        btnDone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoneActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel5.add(btnDone, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 100;
        jPanel5.add(dptDateSearch, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 1, 0, 0);
        add(jPanel5, gridBagConstraints);

        jPanel6.setLayout(new java.awt.GridBagLayout());
        jPanel6.add(txtMeaseage, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        add(jPanel6, gridBagConstraints);
    }// </editor-fold>                        

    public void loadTableRate() {

        vRow.clear();
        Date search = dptDateSearch.getDate();
        lst = ratebuss.getDateRateSearch(search);


        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();

            rat = lst.get(i);
            v.addElement(rat.getDateRate());
            v.addElement(rat.getUsd());
            v.addElement(rat.getVnd());

            vRow.addElement(v);
        }

        Vector vCol = new Vector();
        vCol.addElement("DateRate");
        vCol.addElement("USDRate");
        vCol.addElement("VNDRate");

        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tbRate.setModel(tblModel);
    }

    public void loadAllRate() {
        lst.clear();
        lst = ratebuss.readByAll();
        loadTableRate();
    }

    private void loadTableRateDetail(int index) {

        ArrayList<RateEntity> list = ratebuss.readByAll();
        for (int i = 0; i < lst.size(); i++) {
            if (index != lst.get(i).getRateId()) {
                //lst.remove(i);
                i--;
            }
        }
        loadAllRate();
    }

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        if (dtpDate.getDate() == null || txtUSD.getText().trim().equals("") || txtVietnam.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }
        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New FeeLevel", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {
            rat.setDateRate(dtpDate.getDate());
            rat.setUsd(Float.parseFloat(txtUSD.getText()));
            rat.setVnd(Float.parseFloat(txtVietnam.getText()));

            if (ratebuss.addRate(rat)) {
                JOptionPane.showMessageDialog(this, ratebuss.getLastError());
                dtpDate.setDate(null);
                txtUSD.setText(null);
                txtVietnam.setText(null);

                dtpDate.requestFocus();
                vRow.clear();
                loadAllRate();
                this.tbRate.updateUI();
            } else {
                JOptionPane.showMessageDialog(this, ratebuss.getLastError());
            }
        } else {

            dtpDate.setDate(null);
            txtUSD.setText(null);
            txtVietnam.setText(null);

        }
    }                                      

    private void btnDoneActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        loadTableRate();
        int result = tbRate.getRowCount();
        if (result == 0) {
            txtMeaseage.setText("No results match.");
        } else {
            txtMeaseage.setText(result + " results match.");
        }
        dtpDate.setDate(null);
        txtUSD.setText(null);
        txtVietnam.setText(null);
        dtpDate.requestFocus();
    }                                       

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:

        int selectedIndex = tbRate.getSelectedRow();
        RateEntity rate = lst.get(selectedIndex);
        int RateId = rate.getRateId();
        if (dtpDate.getDate() == null || txtUSD.getText().trim().equals("") || txtVietnam.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }

        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update FeeLevel", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {
            rate.setDateRate(dtpDate.getDate());
            rate.setUsd(Float.parseFloat(txtUSD.getText()));
            rate.setVnd(Float.parseFloat(txtVietnam.getText()));

            if (ratebuss.update(rate)) {
                //note
                JOptionPane.showMessageDialog(this, ratebuss.getLastError());
                vRow.clear();
                loadAllRate();
                this.tbRate.updateUI();
                dtpDate.setDate(null);
                txtUSD.setText(null);
                txtVietnam.setText(null);

            } else {
                JOptionPane.showMessageDialog(this, ratedao.getLastError());
            }
        }
    }                                       

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        int selectedIndex = tbRate.getSelectedRow();
        RateEntity rate = lst.get(selectedIndex);
        int rateId = rate.getRateId();

        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Employee", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {
            if (ratebuss.delete(rateId)) {
                JOptionPane.showMessageDialog(this, ratebuss.getLastError());
                dtpDate.setDate(null);
                txtUSD.setText(null);
                txtVietnam.setText(null);
                vRow.clear();
                loadAllRate();
                this.tbRate.updateUI();
            } else {
                JOptionPane.showMessageDialog(this, ratebuss.getLastError());
            }
        }
    }                                         

    private void tbRateMouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        int selectedIndex = tbRate.getSelectedRow();
        if (selectedIndex > -1) {
            // RateEntity rate = lst.get(selectedIndex);
            txtUSD.setText(String.valueOf(lst.get(selectedIndex).getUsd()));
            txtVietnam.setText(String.valueOf(lst.get(selectedIndex).getVnd()));
            dtpDate.setDate(lst.get(selectedIndex).getDateRate());
        }
    }                                   
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnDone;
    private javax.swing.JButton btnEdit;
    private com.toedter.calendar.JDateChooser dptDateSearch;
    private com.toedter.calendar.JDateChooser dtpDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbRate;
    private javax.swing.JLabel txtMeaseage;
    private javax.swing.JTextField txtUSD;
    private javax.swing.JTextField txtVietnam;
    // End of variables declaration                   

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}


